package com.demo.study.algorithm.search;

import org.junit.Assert;
import org.junit.Test;

import static com.demo.study.algorithm.search.A02_BinarySearch.*;

/**
 * TestClient
 *
 * @author Baymax
 * @since 2024-08-13
 */
public class A02_TestClient {

    @Test
    public void binarySearchBaseTest() {
        int[] array = new int[]{2, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchBase(array, 2));
        Assert.assertEquals(4, binarySearchBase(array, 10));
        Assert.assertEquals(5, binarySearchBase(array, 14));
        Assert.assertEquals(6, binarySearchBase(array, 15));
        Assert.assertEquals(9, binarySearchBase(array, 30));
        Assert.assertEquals(-1, binarySearchBase(array, 0));
        Assert.assertEquals(-1, binarySearchBase(array, 100));
    }

    @Test
    public void binarySearchRevisedTest() {
        int[] array = new int[]{2, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchRevised(array, 2));
        Assert.assertEquals(4, binarySearchRevised(array, 10));
        Assert.assertEquals(5, binarySearchRevised(array, 14));
        Assert.assertEquals(6, binarySearchRevised(array, 15));
        Assert.assertEquals(9, binarySearchRevised(array, 30));
        Assert.assertEquals(-1, binarySearchRevised(array, 0));
        Assert.assertEquals(-1, binarySearchRevised(array, 100));
    }

    @Test
    public void binarySearchBalanceTest() {
        int[] array = new int[]{2, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchBalance(array, 2));
        Assert.assertEquals(4, binarySearchBalance(array, 10));
        Assert.assertEquals(5, binarySearchBalance(array, 14));
        Assert.assertEquals(6, binarySearchBalance(array, 15));
        Assert.assertEquals(9, binarySearchBalance(array, 30));
        Assert.assertEquals(-1, binarySearchBalance(array, 0));
        Assert.assertEquals(-1, binarySearchBalance(array, 100));
    }

    @Test
    public void binarySearchRecursionTest() {
        int[] array = new int[]{2, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchRecursion(array, 2));
        Assert.assertEquals(4, binarySearchRecursion(array, 10));
        Assert.assertEquals(5, binarySearchRecursion(array, 14));
        Assert.assertEquals(6, binarySearchRecursion(array, 15));
        Assert.assertEquals(9, binarySearchRecursion(array, 30));
        Assert.assertEquals(-1, binarySearchRecursion(array, 0));
        Assert.assertEquals(-1, binarySearchRecursion(array, 100));
    }

    @Test
    public void binarySearchLeftMostTest() {
        int[] array = new int[]{2, 6, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchLeftMost(array, 2));
        Assert.assertEquals(5, binarySearchLeftMost(array, 10));
        Assert.assertEquals(6, binarySearchLeftMost(array, 14));
        Assert.assertEquals(7, binarySearchLeftMost(array, 15));
        Assert.assertEquals(1, binarySearchLeftMost(array, 6));
        Assert.assertEquals(8, binarySearchLeftMost(array, 24));
        Assert.assertEquals(10, binarySearchLeftMost(array, 30));
        Assert.assertEquals(-1, binarySearchLeftMost(array, 0));
        Assert.assertEquals(-1, binarySearchLeftMost(array, 100));
    }

    @Test
    public void binarySearchRightMostTest() {
        int[] array = new int[]{2, 6, 6, 6, 6, 10, 14, 15, 24, 24, 30};
        Assert.assertEquals(0, binarySearchRightMost(array, 2));
        Assert.assertEquals(5, binarySearchRightMost(array, 10));
        Assert.assertEquals(6, binarySearchRightMost(array, 14));
        Assert.assertEquals(7, binarySearchRightMost(array, 15));
        Assert.assertEquals(4, binarySearchRightMost(array, 6));
        Assert.assertEquals(9, binarySearchRightMost(array, 24));
        Assert.assertEquals(10, binarySearchRightMost(array, 30));
        Assert.assertEquals(-1, binarySearchRightMost(array, 0));
        Assert.assertEquals(-1, binarySearchRightMost(array, 100));
    }
}
